﻿var app = angular.module('app', []);
app.value('$', $);
app.run(function ($rootScope) {
    $rootScope.nameList = [];
    // $rootScope.nameList.push({ messageStr: "Hello" });
});
app.factory('signalRSvc', function ($, $rootScope) {

    var greeter = $.connection.helloWorldHub;
//    $.connection.hub.logging = true;

    $rootScope.sendRequest = function (message) {
        greeter.server.sendMessage(message);
    };

    greeter.client.displayMessages = function (message) {
        var messageObj = {
            messageStr: message
        };
        $rootScope.nameList.push(messageObj);
        console.log($rootScope.nameList);
        console.log("rootScope id from servise" + $rootScope.$id);

        $rootScope.$digest();
    };



    $rootScope.startConnection = function () {
        performConnection();
    };

    function performConnection() {
        $.connection.hub.start().done(function () {
            console.log("connected");
        });
    }

    performConnection();
});

function SignalRAngularCtrl($scope, signalRSvc, $rootScope) {


    function hello() {
        var input = "hello borka";
        $rootScope.sendRequest(input);
    }

    function startInterval() {
        console.log("started");
        local = setInterval(hello, 2000);
    }

    $("#sendMessageToServer").on("click",function() {
        startInterval();
    });
    
    $("#stopInterval").on("click", function () {
        console.log("stopped");
        clearInterval(local);
    });
}

app.directive("myDirective", function ($rootScope) {
    return {
        restrict: 'C',
        scope: true,
        template: '<ul><li ng-repeat="message in nameList">{{message.messageStr}}</li></ul>',
        link: function (scope, element, attrs, controller) {

            console.log("rootScope id from directive " + $rootScope.$id);
            console.log("scope id from directive " + scope.$id);

        }
    };
});















